package com.xyy.dfapi.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xyy.dfapi.config.ComentCfg;
import com.xyy.dfapi.enums.TaskEnum;
import com.xyy.dfapi.helper.ErpHelper;
import com.xyy.dfapi.pojo.*;
import com.xyy.dfapi.service.ErpService;
import com.xyy.dfapi.service.PopErpTaskService;
import com.xyy.dfapi.service.PopService;
import com.xyy.dfapi.utils.CacheUtils;
import com.xyy.dfapi.utils.Constant;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class PopErpTaskServiceImpl implements PopErpTaskService {

    @Autowired
    private ErpService erpService;
    @Autowired
    private PopService popService;
    private ComentCfg comentCfg;

    {
        comentCfg = JSON.parseObject(CacheUtils.get(Constant.CONFIG).toString(), ComentCfg.class);
    }


    @Override
    public void syncProductFullToPop() {
        ErpResultDto<List<ErpGoodsDto>> erpResultDto = erpService.erpQueryGoods();
        //ERP批次库存信息
        ErpResultDto<List<ErpGoodsStockDto>> listErpResultDto = erpService.erpQueryGoodsStock();
        if (ObjectUtil.isNull(erpResultDto.getResult()) || ObjectUtil.isNull(erpResultDto.getResult())) {
            log.warn("PopErpTaskServiceImpl.syncProductFullToPop#接口查询异常 异常信息: {}", JSON.toJSONString(erpResultDto));
            return;
        }
        if (comentCfg.getIsDone()) {
            log.info("ERP商品查询接口出参: {}", JSON.toJSONString(erpResultDto));
        } else {
            log.info("ERP商品上报: {}条", erpResultDto.getResult().size());
        }
        if (erpResultDto.getStatus() != 200 || listErpResultDto.getStatus() != 200) {
            log.warn("PopErpTaskServiceImpl.syncProductFullToPop#接口返回状态异常 result: {}", JSON.toJSONString(erpResultDto));
            return;
        }
        //ERP商品全量数据
        List<ErpGoodsDto> erpGoodsDtoList = JSON.parseArray(JSON.toJSONString(erpResultDto.getResult()), ErpGoodsDto.class);
        List<ErpGoodsStockDto> erpGoodsStockDtoList = JSON.parseArray(JSON.toJSONString(listErpResultDto.getResult()), ErpGoodsStockDto.class);
        List<PopErpSkuForInsertDto> skuForInsertDtos = ErpHelper.convertErpToPopSkuDtos(erpGoodsDtoList, erpGoodsStockDtoList);
        TaskQueryParam taskQueryParam = new TaskQueryParam();
        taskQueryParam.setJsonData(JSON.toJSONString(skuForInsertDtos));
        taskQueryParam.setTaskId(TaskEnum.PRODUCT_FULL.getTaskId());
        taskQueryParam.setAppId(comentCfg.getPopAppId());
        taskQueryParam.setOrgId(comentCfg.getPopOrgId());
        taskQueryParam.setAppSecret(comentCfg.getPopAppSecret());
        popService.pushDataInfo(taskQueryParam);
    }

    @Override
    public void syncProductStockToPop() {
        TaskQueryParam taskQueryParam = new TaskQueryParam();
        taskQueryParam.setTaskId(TaskEnum.PRODUCT_STOCK.getTaskId());
        taskQueryParam.setAppId(comentCfg.getPopAppId());
        taskQueryParam.setOrgId(comentCfg.getPopOrgId());
        taskQueryParam.setAppSecret(comentCfg.getPopAppSecret());
        //未查询到库存数据  重置为0
        ApiRPCResult<ResultDto> resultDtoApiRPCResult = popService.queryTaskCondition(taskQueryParam);
        ResultDto data = JSON.parseObject(JSON.toJSONString(resultDtoApiRPCResult.getData()), ResultDto.class);
        if (ObjectUtil.isEmpty(data)) {
            log.warn("PopErpTaskServiceImpl.syncOrderStatePop# 【当前没有需要同步库存商品】");
            return;
        }
        QueryTaskData queryTaskData = JSON.parseObject(data.getDataJson(), QueryTaskData.class);
        //ERP批次库存信息
        ErpResultDto<List<ErpGoodsStockDto>> listErpResultDto = erpService.erpQueryGoodsStock();
        if (ObjectUtil.isNull(listErpResultDto.getResult())) {
            log.warn("PopErpTaskServiceImpl.syncProductStockToPop#库存接口查询异常 异常信息: {}", JSON.toJSONString(listErpResultDto));
            return;
        }
        if (comentCfg.getIsDone()) {
            log.info("ERP库存查询接口出参: {}", JSON.toJSONString(listErpResultDto));
        } else {
            log.info("ERP库存上报: {}条", listErpResultDto.getResult().size());
        }
        if (listErpResultDto.getStatus() != 200 || !listErpResultDto.getMessage().contains("操作成功")) {
            log.warn("PopErpTaskServiceImpl.syncProductStockToPop#接口返回状态异常 result: {}", JSON.toJSONString(listErpResultDto));
            return;
        }
        List<ErpGoodsStockDto> erpGoodsStockDtoList = JSON.parseArray(JSON.toJSONString(listErpResultDto.getResult()), ErpGoodsStockDto.class);
        //获取到ERP库存
        List<PopErpSkuStockV2Dto> popSkuStock = ErpHelper.convertErpToPopSkuStock(erpGoodsStockDtoList);
        List<String> erpCodes = popSkuStock.stream().map(m -> m.getErpCode()).collect(Collectors.toList());
        //库存置0操作
        List<String> listQueryParam = queryTaskData.getListQueryParam();
        //过滤出需要置0商品
        listQueryParam.removeAll(erpCodes);
        //置0
        List<PopErpSkuStockV2Dto> zeroStock = setToZero(listQueryParam);
        popSkuStock.addAll(zeroStock);
        taskQueryParam.setJsonData(JSON.toJSONString(popSkuStock));
        popService.pushDataInfo(taskQueryParam);
    }

    private List<PopErpSkuStockV2Dto> setToZero(List<String> listQueryParam) {
        List<PopErpSkuStockV2Dto> list = new ArrayList<>();
        for (String erpCode : listQueryParam) {
            PopErpSkuStockV2Dto popErpSkuStockV2Dto = new PopErpSkuStockV2Dto();
            popErpSkuStockV2Dto.setErpCode(erpCode);
            popErpSkuStockV2Dto.setAvailableQyt(0);
            list.add(popErpSkuStockV2Dto);
        }
        return list;
    }


    @Override
    public void syncProductPriceToPop() {
        ErpResultDto<List<ErpGoodsDto>> erpResultDto = erpService.erpQueryGoods();
        if (ObjectUtil.isNull(erpResultDto.getResult())) {
            log.warn("PopErpTaskServiceImpl.syncProductStockToPop#价格接口查询异常 异常信息: {}", JSON.toJSONString(erpResultDto));
            return;
        }
        if (comentCfg.getIsDone()) {
            log.info("ERP商品查询接口出参: {}", JSON.toJSONString(erpResultDto));
        } else {
            log.info("ERP价格上报: {}条", erpResultDto.getResult().size());
        }
        if (erpResultDto.getStatus() != 200) {
            log.warn("PopErpTaskServiceImpl.syncProductPriceToPop#接口返回状态异常 result: {}", JSON.toJSONString(erpResultDto));
            return;
        }
        //ERP商品全量数据
        List<ErpGoodsDto> erpGoodsDtoList = JSON.parseArray(JSON.toJSONString(erpResultDto.getResult()), ErpGoodsDto.class);
        List<PopErpSkuPriceDto> skuPriceDtoList = ErpHelper.convertErpToPopSkuPrice(erpGoodsDtoList);
        TaskQueryParam taskQueryParam = new TaskQueryParam();
        taskQueryParam.setJsonData(JSON.toJSONString(skuPriceDtoList));
        taskQueryParam.setTaskId(TaskEnum.PRODUCT_PRICE.getTaskId());
        taskQueryParam.setAppId(comentCfg.getPopAppId());
        taskQueryParam.setOrgId(comentCfg.getPopOrgId());
        taskQueryParam.setAppSecret(comentCfg.getPopAppSecret());
        popService.pushDataInfo(taskQueryParam);
    }

    @Override
    public void syncCustomerToPop() {
        //客户接口返回信息
        ErpResultDto listErpResultDto = new ErpResultDto<>();
        //ERP客户信息
        List<ErpCustDto> erpCustDtoList = new ArrayList<>();
        if (comentCfg.getIsFullSync()) {
            listErpResultDto = erpService.erpQueryCus();
            if (ObjectUtil.isNull(listErpResultDto.getResult())) {
                log.warn("PopErpTaskServiceImpl.syncCustomerToPop#客户全量接口查询异常 异常信息: {}", JSON.toJSONString(listErpResultDto));
                return;
            }
            erpCustDtoList = JSON.parseArray(JSON.toJSONString(listErpResultDto.getResult()), ErpCustDto.class);
        } else {
            listErpResultDto = erpService.queryCusPage(1, 2000);
            if (ObjectUtil.isNull(listErpResultDto.getResult())) {
                log.warn("PopErpTaskServiceImpl.syncCustomerToPop#客户分页接口查询异常 异常信息: {}", JSON.toJSONString(listErpResultDto));
                return;
            }
            JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(listErpResultDto.getResult()));
            erpCustDtoList = JSON.parseArray(JSON.toJSONString(jsonObject.get("list")), ErpCustDto.class);
        }
        if (listErpResultDto.getStatus() != 200) {
            log.warn("PopErpTaskServiceImpl.syncCustomerToPop#接口返回状态异常 result: {}", JSON.toJSONString(listErpResultDto));
            return;
        }
        if (comentCfg.getIsDone()) {
            log.info("ERP 客户查询接口出参: {}", JSON.toJSONString(listErpResultDto));
        } else {
            log.info("ERP上报客户信息: {}条", erpCustDtoList.size());
        }
        List<PopErpClientInfoDto> popErpClientInfoDtoList = ErpHelper.convertErpToPopCustomer(erpCustDtoList);
        TaskQueryParam taskQueryParam = new TaskQueryParam();
        taskQueryParam.setJsonData(JSON.toJSONString(popErpClientInfoDtoList));
        taskQueryParam.setTaskId(TaskEnum.CLIENT_INFO.getTaskId());
        taskQueryParam.setAppId(comentCfg.getPopAppId());
        taskQueryParam.setOrgId(comentCfg.getPopOrgId());
        taskQueryParam.setAppSecret(comentCfg.getPopAppSecret());
        popService.pushDataInfo(taskQueryParam);
    }

    @Override
    public void syncOrderToMerchant() {
        TaskQueryParam taskQueryParam = new TaskQueryParam();
        taskQueryParam.setTaskId(TaskEnum.ORDER_TOTAL.getTaskId());
        taskQueryParam.setAppId(comentCfg.getPopAppId());
        taskQueryParam.setOrgId(comentCfg.getPopOrgId());
        taskQueryParam.setAppSecret(comentCfg.getPopAppSecret());
        ApiRPCResult<ResultDto> resultDtoApiRPCResult = popService.pullDataInfo(taskQueryParam);
        ResultDto data = JSON.parseObject(JSON.toJSONString(resultDtoApiRPCResult.getData()), ResultDto.class);
        if (ObjectUtil.isEmpty(data)) {
            log.warn("PopErpTaskServiceImpl.syncNoneOpenCustomerToMerchant# 【当前没有可同步订单数据】");
            return;
        }
        List<SellerOrderInfoDto> sellerOrderInfoDtos = JSON.parseArray(data.getDataJson(), SellerOrderInfoDto.class);

        List<SellerOrderCallBackDto> sellerOrderCallBackDtoList = new ArrayList<>();
        for (SellerOrderInfoDto popOrder : sellerOrderInfoDtos) {
            ErpResultDto erpResultDto = erpService.insertOrder(ErpHelper.convertPopToErpOrder(popOrder));
            if (ObjectUtil.isNull(erpResultDto.getResult()) && !"该订单号订单已经存在".equals(erpResultDto.getMessage())) {
                log.warn("PopErpTaskServiceImpl.syncNoneOpenCustomerToMerchant#订单插入接口异常# 异常订单: {},异常信息: {}", JSON.toJSONString(popOrder), JSON.toJSONString(erpResultDto));
                continue;
            }
            if (erpResultDto.getStatus() == 200 || "该订单号订单已经存在".equals(erpResultDto.getMessage())) {
                SellerOrderCallBackDto sellerOrderCallBackDto = new SellerOrderCallBackDto();
                sellerOrderCallBackDto.setOrderNo(popOrder.getOrderNo());
                sellerOrderCallBackDto.setResult(true);
                sellerOrderCallBackDto.setOrgId(comentCfg.getPopOrgId());
                sellerOrderCallBackDto.setType(1);
                sellerOrderCallBackDtoList.add(sellerOrderCallBackDto);
            } else {
                log.info("PopErpTaskServiceImpl.syncNoneOpenCustomerToMerchant#订单导入失败 erpResult: {} 订单号: {}", JSON.toJSONString(erpResultDto), popOrder.getOrderNo());
            }

        }
        taskQueryParam.setTaskId(10086);
        taskQueryParam.setJsonData(JSON.toJSONString(sellerOrderCallBackDtoList));
        if (ObjectUtil.isNotEmpty(sellerOrderCallBackDtoList)) {
            popService.callbackForPullTask(taskQueryParam);
            log.info("订单回调成功: {}", JSON.toJSONString(sellerOrderCallBackDtoList.stream().map(m -> m.getOrderNo()).collect(Collectors.toList())));
        }
    }

    @Override
    public void syncNoneOpenCustomerToMerchant() {
        TaskQueryParam taskQueryParam = new TaskQueryParam();
        taskQueryParam.setTaskId(TaskEnum.CLIENT_PUSH_DOWN.getTaskId());
        taskQueryParam.setAppId(comentCfg.getPopAppId());
        taskQueryParam.setOrgId(comentCfg.getPopOrgId());
        taskQueryParam.setAppSecret(comentCfg.getPopAppSecret());
        ApiRPCResult<ResultDto> resultDtoApiRPCResult = popService.pullDataInfo(taskQueryParam);
        ResultDto data = JSON.parseObject(JSON.toJSONString(resultDtoApiRPCResult.getData()), ResultDto.class);
        if (ObjectUtil.isEmpty(data)) {
            log.warn("PopErpTaskServiceImpl.syncNoneOpenCustomerToMerchant# 【当前没有可写入客户数据】");
            return;
        }
        List<PlatformCustomerDto> platformCustomerDtos = JSON.parseArray(data.getDataJson(), PlatformCustomerDto.class);
        List<SellerCustomerCallBackDto> callCustomer = new ArrayList<>();
        for (PlatformCustomerDto platformCustomerDto : platformCustomerDtos) {
            ErpResultDto erpResultDto = erpService.insertCustomer(ErpHelper.convertPopToErpCustomer(platformCustomerDto));
            if (ObjectUtil.isNull(erpResultDto.getResult()) && !"该单位ID已经存在".equals(erpResultDto.getMessage())) {
                log.warn("PopErpTaskServiceImpl.syncNoneOpenCustomerToMerchant#客户插入接口异常# 异常客户: {},异常信息: {}", JSON.toJSONString(platformCustomerDto), JSON.toJSONString(erpResultDto));
                continue;
            }
            if (erpResultDto.getStatus() == 200 || "该单位ID已经存在".equals(erpResultDto.getMessage())) {
                SellerCustomerCallBackDto customerCallBackDto = new SellerCustomerCallBackDto();
                customerCallBackDto.setId(platformCustomerDto.getId());
                customerCallBackDto.setNo(platformCustomerDto.getCustomerName());
                customerCallBackDto.setResult(true);
                customerCallBackDto.setOrgId(comentCfg.getPopOrgId());
                callCustomer.add(customerCallBackDto);
            } else {
                log.info("PopErpTaskServiceImpl.syncNoneOpenCustomerToMerchant#客户导入失败 erpResult: {}", JSON.toJSONString(erpResultDto));
            }
        }
        taskQueryParam.setTaskId(10086);
        taskQueryParam.setJsonData(JSON.toJSONString(callCustomer));
        if (ObjectUtil.isNotEmpty(callCustomer)) {
            popService.callbackForCustomerPullTask(taskQueryParam);
            log.info("客户回调成功: {}", JSON.toJSONString(callCustomer.stream().map(m -> m.getNo()).collect(Collectors.toList())));
        }

    }

    /**
     * 出库信息同步
     */
    @SneakyThrows
    @Override
    public void syncOrderStatePop() {
        syncCustomerToPop();
        TaskQueryParam taskQueryParam = new TaskQueryParam();
        taskQueryParam.setTaskId(TaskEnum.ORDER_DELIVERY_DETAILE.getTaskId());
        taskQueryParam.setAppId(comentCfg.getPopAppId());
        taskQueryParam.setOrgId(comentCfg.getPopOrgId());
        taskQueryParam.setAppSecret(comentCfg.getPopAppSecret());
        ApiRPCResult<ResultDto> resultDtoApiRPCResult = popService.queryTaskCondition(taskQueryParam);
        ResultDto data = JSON.parseObject(JSON.toJSONString(resultDtoApiRPCResult.getData()), ResultDto.class);
        if (ObjectUtil.isEmpty(data)) {
            log.warn("PopErpTaskServiceImpl.syncOrderStatePop# 【当前没有需要同步状态订单】");
            return;
        }
        QueryTaskData queryTaskData = JSON.parseObject(data.getDataJson(), QueryTaskData.class);
        List<SellerOrderDeliverDetailForErpDto> orderDeliverDetailForErpDtoList = new ArrayList<>();
        for (String orderNo : queryTaskData.getListQueryParam()) {
            ErpResultDto erpResultDto = erpService.queryOrderState(orderNo);
            if (ObjectUtil.isNull(erpResultDto.getResult())) {
                log.warn("PopErpTaskServiceImpl.syncOrderStatePop#出库信息接口查询异常# 异常单号: {},异常信息: {}", orderNo, JSON.toJSONString(erpResultDto));
                continue;
            }
            if (erpResultDto.getStatus() == 200 || erpResultDto.getMessage().contains("操作成功")) {
                log.info("ERP订单状态接口【待配送】 当前查询订单: {} 查询结果: {}", orderNo, JSON.toJSONString(erpResultDto));
                ErpOrderStateDto erpOrderStateDto = JSON.parseObject(JSON.toJSONString(erpResultDto.getResult()), ErpOrderStateDto.class);
                if (ObjectUtil.isNotEmpty(erpOrderStateDto.getState()) && ObjectUtil.equals("出库记账", erpOrderStateDto.getState())) {
                    orderDeliverDetailForErpDtoList.addAll(ErpHelper.convertErpToPopOrderOutDetail(erpOrderStateDto));
                }
            }
        }
        taskQueryParam.setJsonData(JSON.toJSONString(orderDeliverDetailForErpDtoList));
        if (CollectionUtil.isEmpty(orderDeliverDetailForErpDtoList)) {
            return;
        }
        popService.pushDataInfo(taskQueryParam);
    }

    /**
     * 物流信息同步
     */
    @SneakyThrows
    @Override
    public void syncOrderDeliverInfo() {
        TaskQueryParam taskQueryParam = new TaskQueryParam();
        taskQueryParam.setTaskId(TaskEnum.ORDER_DELIVERY_WMS.getTaskId());
        taskQueryParam.setAppId(comentCfg.getPopAppId());
        taskQueryParam.setOrgId(comentCfg.getPopOrgId());
        taskQueryParam.setAppSecret(comentCfg.getPopAppSecret());
        ApiRPCResult<ResultDto> resultDtoApiRPCResult = popService.queryTaskCondition(taskQueryParam);
        ResultDto data = JSON.parseObject(JSON.toJSONString(resultDtoApiRPCResult.getData()), ResultDto.class);
        if (ObjectUtil.isEmpty(data)) {
            log.warn("PopErpTaskServiceImpl.syncOrderStatePop# 【当前没有需要同步物流订单】");
            return;
        }
        QueryTaskData queryTaskData = JSON.parseObject(data.getDataJson(), QueryTaskData.class);
        List<SellerOrderDeliverInfoDto> sellerOrderDeliverInfoDtoList = new ArrayList<>();
        for (String orderNo : queryTaskData.getListQueryParam()) {
            ErpResultDto erpResultDto = erpService.queryOrderState(orderNo);
            if (ObjectUtil.isNull(erpResultDto.getResult())) {
                log.warn("PopErpTaskServiceImpl.syncOrderStatePop#物流信息接口查询异常# 异常单号: {},异常信息: {}", orderNo, JSON.toJSONString(erpResultDto));
                continue;
            }
            if ((erpResultDto.getStatus() == 200 || erpResultDto.getMessage().contains("操作成功")) && ObjectUtil.isNotEmpty(erpResultDto.getResult())) {
                log.info("ERP物流接口【物流信息】 查询单号: {} 查询结果: {}", orderNo, JSON.toJSONString(erpResultDto));
                ErpOrderStateDto erpOrderStateDto = JSON.parseObject(JSON.toJSONString(erpResultDto.getResult()), ErpOrderStateDto.class);
                if (ObjectUtil.isNotEmpty(erpOrderStateDto.getLogisticsId())) {
                    sellerOrderDeliverInfoDtoList.add(ErpHelper.convertErpToPopOrderDeliver(erpOrderStateDto, comentCfg.getPopOrgId()));
                }
            }
        }
        taskQueryParam.setJsonData(JSON.toJSONString(sellerOrderDeliverInfoDtoList));
        if (CollectionUtil.isEmpty(sellerOrderDeliverInfoDtoList)) {
            return;
        }
        popService.pushDataInfo(taskQueryParam);
    }

    /**
     * 电子发票
     */
    @SneakyThrows
    @Override
    public void syncOrderInvoice() {
        TaskQueryParam taskQueryParam = new TaskQueryParam();
        taskQueryParam.setTaskId(TaskEnum.INVOICE_RETURN.getTaskId());
        taskQueryParam.setAppId(comentCfg.getPopAppId());
        taskQueryParam.setOrgId(comentCfg.getPopOrgId());
        taskQueryParam.setAppSecret(comentCfg.getPopAppSecret());
        ApiRPCResult<ResultDto> resultDtoApiRPCResult = popService.queryTaskCondition(taskQueryParam);
        ResultDto data = JSON.parseObject(JSON.toJSONString(resultDtoApiRPCResult.getData()), ResultDto.class);
        if (ObjectUtil.isEmpty(data)) {
            log.warn("PopErpTaskServiceImpl.syncOrderStatePop# 【当前没有需要同步电子发票订单】");
            return;
        }
        QueryTaskData queryTaskData = JSON.parseObject(data.getDataJson(), QueryTaskData.class);
        List<PopErpInvoiceDto> orderInvoices = new ArrayList<>();
        for (String orderNo : queryTaskData.getListQueryParam()) {
            ErpResultDto erpResultDto = erpService.queryOrderInvoice(orderNo);
            if (ObjectUtil.isNull(erpResultDto.getResult())) {
                log.warn("PopErpTaskServiceImpl.syncOrderStatePop# 电子发票接口查询异常# 异常单号: {},异常信息: {}", orderNo, JSON.toJSONString(erpResultDto));
                continue;
            }
            if ((erpResultDto.getStatus() == 200 || erpResultDto.getMessage().contains("操作成功")) && ObjectUtil.isNotEmpty(erpResultDto.getResult())) {
                log.info("ERP发票接口【电子发票】 查询单号: {} 查询结果: {}", orderNo, JSON.toJSONString(erpResultDto));
                ErpOrderInvoiceDto erpOrderInvoiceDto = JSON.parseObject(JSON.toJSONString(erpResultDto.getResult()), ErpOrderInvoiceDto.class);
                if (ObjectUtil.isNotEmpty(erpOrderInvoiceDto.getPdfUrl())) {
                    orderInvoices.add(ErpHelper.convertErpToPopOrderInvoice(erpOrderInvoiceDto));
                }
            }
        }
        taskQueryParam.setJsonData(JSON.toJSONString(orderInvoices));
        if (CollectionUtil.isEmpty(orderInvoices)) {
            return;
        }
        popService.pushDataInfo(taskQueryParam);
    }

    /**
     * 已开单
     */
    @SneakyThrows
    @Override
    public void syncOrderDone() {
        TaskQueryParam taskQueryParam = new TaskQueryParam();
        taskQueryParam.setTaskId(TaskEnum.ORDER_COMPLETE.getTaskId());
        taskQueryParam.setAppId(comentCfg.getPopAppId());
        taskQueryParam.setOrgId(comentCfg.getPopOrgId());
        taskQueryParam.setAppSecret(comentCfg.getPopAppSecret());
        ApiRPCResult<ResultDto> resultDtoApiRPCResult = popService.queryTaskCondition(taskQueryParam);
        ResultDto data = JSON.parseObject(JSON.toJSONString(resultDtoApiRPCResult.getData()), ResultDto.class);
        if (ObjectUtil.isEmpty(data)) {
            log.warn("PopErpTaskServiceImpl.syncOrderStatePop# 【当前没有需要同步分拣中状态订单】");
            return;
        }
        QueryTaskData queryTaskData = JSON.parseObject(data.getDataJson(), QueryTaskData.class);
        List<PopErpOrderCompleteDto> popErpOrderCompleteDtoList = new ArrayList<>();
        for (String orderNo : queryTaskData.getListQueryParam()) {
            ErpResultDto erpResultDto = erpService.queryOrderState(orderNo);
            if (ObjectUtil.isNull(erpResultDto.getResult())) {
                log.warn("PopErpTaskServiceImpl.syncOrderStatePop# 已开单接口查询异常# 异常单号: {},异常信息: {}", orderNo, JSON.toJSONString(erpResultDto));
                continue;
            }
            if ((erpResultDto.getStatus() == 200 || erpResultDto.getMessage().contains("操作成功")) && ObjectUtil.isNotEmpty(erpResultDto.getResult())) {
                log.info("ERP订单状态接口【已开单】 查询单号: {} 查询结果: {}", orderNo, JSON.toJSONString(erpResultDto));
                ErpOrderStateDto erpOrderStateDto = JSON.parseObject(JSON.toJSONString(erpResultDto.getResult()), ErpOrderStateDto.class);
                if (ObjectUtil.isNotEmpty(erpOrderStateDto.getState()) && ObjectUtil.equals("审核通过", erpOrderStateDto.getState())) {
                    popErpOrderCompleteDtoList.add(ErpHelper.convertErpToPopOrderDone(erpOrderStateDto));
                }
            }
        }
        taskQueryParam.setJsonData(JSON.toJSONString(popErpOrderCompleteDtoList));
        if (CollectionUtil.isEmpty(popErpOrderCompleteDtoList)) {
            return;
        }
        popService.pushDataInfo(taskQueryParam);
    }


}
